{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "标准`JackCompiler.sh`生成的文件，我将其命名为`Xxx_.vm`。  \n",
    "这样，我们就可以使用下面的比较代码，将我们生成的vm文件和标准工具生成的文件进行对比，以此来确认我们的编译正确与否。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "def diff(folder:str):\n",
    "    jack_folder = folder\n",
    "    jack_files:list[str] = []\n",
    "    file_names = os.listdir(jack_folder)\n",
    "    for file_name in file_names:\n",
    "        if file_name.endswith(\".jack\"):\n",
    "            jack_files.append(file_name)\n",
    "\n",
    "    success = True\n",
    "    for jack_file in jack_files:\n",
    "        jack_name, jack_suffix = os.path.splitext(jack_file)\n",
    "        vm_file = f\"{jack_name}.vm\"\n",
    "        _vm_file = f\"{jack_name}_.vm\"\n",
    "        with open(f\"{jack_folder}/{vm_file}\", \"r\") as vm_obj:\n",
    "            vm_lines = vm_obj.readlines()\n",
    "        with open(f\"{jack_folder}/{_vm_file}\", \"r\") as vm_my_obj:\n",
    "            vm_my_lines = vm_my_obj.readlines()\n",
    "\n",
    "        for i in range(len(vm_lines)):\n",
    "            if vm_lines[i] != vm_my_lines[i]:\n",
    "                success = False\n",
    "                print(f\"文件{vm_file}和{_vm_file}，在{i+1}行内容不一致\")\n",
    "                break\n",
    "        if success:\n",
    "            print(f\"文件{jack_name}比较成功\")\n",
    "    if success:\n",
    "        print(\"全部比较成功\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "文件Main比较成功\n",
      "全部比较成功\n"
     ]
    }
   ],
   "source": [
    "diff(\"Average\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "文件Main比较成功\n",
      "全部比较成功\n"
     ]
    }
   ],
   "source": [
    "diff(\"ComplexArrays\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "文件Main比较成功\n",
      "全部比较成功\n"
     ]
    }
   ],
   "source": [
    "diff(\"ConvertToBin\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "文件Ball比较成功\n",
      "文件Bat比较成功\n",
      "文件Main比较成功\n",
      "文件PongGame比较成功\n",
      "全部比较成功\n"
     ]
    }
   ],
   "source": [
    "diff(\"Pong\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "文件Main比较成功\n",
      "全部比较成功\n"
     ]
    }
   ],
   "source": [
    "diff(\"Seven\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "文件Main比较成功\n",
      "文件Square比较成功\n",
      "文件SquareGame比较成功\n",
      "全部比较成功\n"
     ]
    }
   ],
   "source": [
    "diff(\"Square\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
